package nexus.manager
{
	import com.ux.manager.StageManager;
	
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	
	/**
	 * <br>설명 : </br> 
	 * @author winnie
	 */	
	public class ToolTipManager
	{
		
		private static var _instance : ToolTipManager;

		public static function get instance():ToolTipManager
		{
			if( _instance == null ) _instance = new ToolTipManager( new SingletonEnforcer() );
			return _instance;
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// private vars
		//
		//-----------------------------------------------------------------------------------
		
		private var _tooltipField : TextField;
		private var _defaultTooltipWidth : Number = -1;
		//-----------------------------------------------------------------------------------
		//
		// protected vars
		//
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		//
		// public vars
		//
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// constructor
		//
		//-----------------------------------------------------------------------------------
		public function ToolTipManager( se : SingletonEnforcer )
		{
			if( se == null ) throw new Error( "생성하여 사용할 수 없는 객체입니다." );
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// setter / getter
		//
		//-----------------------------------------------------------------------------------
		public function get tooltipField():TextField
		{
			if( _tooltipField == null )
			{
				_tooltipField = new TextField();
				_tooltipField.width = defaultTooltipWidth;
				_tooltipField.wordWrap = true;
				_tooltipField.multiline = true;
				_tooltipField.background = true;
				_tooltipField.backgroundColor = 0xffffff;
				_tooltipField.border = true;
				_tooltipField.borderColor = 0xcccccc;
				
				var tf : TextFormat = new TextFormat( "돋움", 12, 0x666666 );
				tf.leading = 5;
				_tooltipField.setTextFormat( tf );
				_tooltipField.defaultTextFormat = tf;
			}
			
			return _tooltipField;
		}
		
		public function set tooltipField(value:TextField):void
		{
			_tooltipField = value;
		}

		public function get defaultTooltipWidth():Number
		{
			if( _defaultTooltipWidth == -1 )
			{
				_defaultTooltipWidth = 300;
			}
			return _defaultTooltipWidth;
		}
		
		public function set defaultTooltipWidth(value:Number):void
		{
			_defaultTooltipWidth = value;
		}

		//-----------------------------------------------------------------------------------
		//
		// public function
		//
		//-----------------------------------------------------------------------------------
		
		public function showTooltip( msg:String ):void
		{
			tooltipField.text = msg;
			if( tooltipField.numLines == 1 && tooltipField.textWidth + 4 < defaultTooltipWidth )
			{
				tooltipField.width = tooltipField.textWidth + 4;
				tooltipField.height = tooltipField.textHeight + 4;
			}
			else
			{
				tooltipField.height = tooltipField.textHeight + 4;
			}
			
			if( StageManager.stage )
			{
				StageManager.stage.addChild( tooltipField );
				setTooltipPosition();
			}
		}
		
		public function hideTooltip():void
		{
			if( StageManager.stage && StageManager.stage.contains( tooltipField )  )
			{
				if( tooltipField.hitTestPoint( StageManager.stage.mouseX, StageManager.stage.mouseY, true ) == true )
				{
					tooltipField.addEventListener(MouseEvent.ROLL_OUT, onToolTipOut );
				}
				else
				{
					removeToolTip();
				}
			}
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// protected function
		//
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// private function
		//
		//-----------------------------------------------------------------------------------
		
		private function setTooltipPosition():void
		{
			tooltipField.x = StageManager.stage.mouseX;
			tooltipField.y = StageManager.stage.mouseY;
		}
		
		private function removeToolTip():void
		{
			if( StageManager.stage && StageManager.stage.contains( tooltipField )  )
			{
				StageManager.stage.removeChild( tooltipField );
				tooltipField.width = defaultTooltipWidth;
			}
		}
		
		//-----------------------------------------------------------------------------------
		//
		// event handler
		//
		//-----------------------------------------------------------------------------------
		
		
		protected function onToolTipOut(e:MouseEvent):void
		{
			tooltipField.removeEventListener(MouseEvent.ROLL_OUT, onToolTipOut );
			removeToolTip();
		}		
		
		//-----------------------------------------------------------------------------------
		//
		// destory
		//
		//-----------------------------------------------------------------------------------
	}
}

class SingletonEnforcer{}
